<script setup>
import { onMounted, reactive, ref } from 'vue';
import { uniFetch } from '../../apis/uni-fetch';
import { showFailToast, showToast } from '../../utils';
import { getLoginUser, getMyTask } from '../../apis/user';
import { useMyStore } from '../../stores';
import {onShow} from '@dcloudio/uni-app'


const taskInfo = ref({})
const user = ref({})
const getTaskInfo = () => {
  let date = new Date()
  let m = date.getMonth() + 1;
  if(m < 10){
    m = '0' + m
  }
  getMyTask(date.getFullYear(),m,res => {
    if(res.code === 200){
      taskInfo.value = res.data
    }
  })
}

onShow(async () => {
  await getLoginUser(res => {
    console.log('getLoginUser',res);
    user.value = res
  })
  getTaskInfo()
})

onMounted(async () => {
  const storeObj = useMyStore()
  user.value = {...storeObj.user}
  if(!storeObj.token || storeObj.token.length === 0){
    return uni.navigateTo({
      url:'/pages/login/index'
    })
  }

  getTaskInfo()
  await getLoginUser(res => {
    console.log('setLoginUser',res);
    user.value = res
    storeObj.setLoginUser(res)
  })
})
</script>

<template>
  <view class="page-container">
    <view class="user-profile">
      <image class="avatar" :src="user.avatar" mode=""></image>
      <text class="username">{{user.name}}</text>
      <text class="no">司机编号：{{user.number}}</text>
      <text class="mobile">手机号码：{{user.phone}}</text>
    </view>
    <view class="month-overview">
      <view class="title">我的任务</view>
      <view class="content">
        <view class="item">
          <text class="volumn">{{taskInfo.taskAmounts}}</text>
          <text class="label">任务总量</text>
        </view>
        <view class="item">
          <text class="volumn">{{taskInfo.completedAmounts}}</text>
          <text class="label">完成任务量</text>
        </view>
        <view class="item">
          <text class="volumn">{{taskInfo.transportMileage}}</text>
          <text class="label">运输里程(km)</text>
        </view>
      </view>
    </view>
    <view class="entry-list">
      <uni-list :border="false">
        <uni-list-item
          to="/subpkg_user/truck/index"
          showArrow
          title="车辆信息"
        />
        <uni-list-item
          to="/subpkg_user/task/index"
          showArrow
          title="任务数据"
        />
        <uni-list-item
          to="/subpkg_user/settings/index"
          showArrow
          title="系统设置"
        />
      </uni-list>
    </view>
  </view>
</template>

<style lang="scss" scoped>
  @import './index.scss';
</style>
